package com.rchat.pocmini.activity.register;

import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.view.ClearEditText;
import com.rchat.pocmini.common.mvp.BaseMvpActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ljd on 2017/7/27.
 */

public class RegisterMvpActivity extends BaseMvpActivity<RegisterPresenterImpl, RegisterModelImpl> implements RegisterContract.View {

    /**
     * 账号输入框
     */
    @BindView(R.id.et_nickname)
    ClearEditText mEtNickname;

    /**
     * 密码输入框
     */
    @BindView(R.id.et_pwd)
    ClearEditText mEtPassword;

    /**
     * 确认密码输入框
     */
    @BindView(R.id.et_pwd_confirm)
    ClearEditText mEtPasswordConfirm;

    /**
     * 邮箱输入框
     */
    @BindView(R.id.et_email)
    ClearEditText mEtEmail;

    /**
     * 手机号码输入框
     */
    @BindView(R.id.et_phone)
    ClearEditText mEtPhone;

    /**
     * 验证码输入框
     */
    @BindView(R.id.et_identify_code)
    ClearEditText mEtIdentifyCode;

    /**
     * 用户名输入框
     */
    @BindView(R.id.et_username)
    ClearEditText mEtUserName;

    /**
     * 获取验证码点击按钮
     */
    @BindView(R.id.btn_identify_code_timer)
    Button mBtnIdentifyCodeTimer;

    /**
     * 进度条
     */
    @BindView(R.id.pb_register)
    ProgressBar mPbRegister;

    /**
     * 获取验证码的60秒倒计时器
     */
    TimeCount mTimeCount;

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public int getLayoutId() {
        return R.layout.register_activity_layout;
    }

    @Override
    public void initView() {

        mTimeCount = new TimeCount(60000, 1000);

        //用户输完用户名后会静默去服务器查询此用户名是否已经被注册
        mEtUserName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    mPresenter.checkAccount(mEtUserName.getText().toString().trim());
                }
            }
        });

        //当用户输入的手机号码位数达到11位时，获取验证码按钮才可点击
        mEtPhone.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 11) {
                    mBtnIdentifyCodeTimer.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_identify_code_normal));
                } else {
                    mBtnIdentifyCodeTimer.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_identify_code_unnormal));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (temp.length() == 11) {
                    mBtnIdentifyCodeTimer.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_identify_code_normal));
                    mBtnIdentifyCodeTimer.setClickable(true);
                } else {
                    mBtnIdentifyCodeTimer.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_identify_code_unnormal));
                    mBtnIdentifyCodeTimer.setClickable(false);
                }
            }
        });
    }

    @Override
    public void doAfterInitView() {

    }

    @Override
    protected void onDestroy() {
        if (mTimeCount != null) {
            mTimeCount.cancel();
        }
        super.onDestroy();
    }

    @Override
    public void setProcessBarVisibility(int visibility) {
        mPbRegister.setVisibility(visibility);
    }


    @OnClick({R.id.btn_identify_code_timer,
            R.id.iv_register,
            R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_identify_code_timer:
                //获取验证码
                mPresenter.getVerificationCode(
                        mEtUserName.getText().toString().trim(),
                        mEtNickname.getText().toString().trim(),
                        mEtPassword.getText().toString().trim(),
                        mEtPasswordConfirm.getText().toString().trim(),
                        mEtPhone.getText().toString().trim());
                break;
            case R.id.iv_register:
                //注册
                mPresenter.register(mEtNickname.getText().toString().trim(),
                        mEtUserName.getText().toString().trim(),
                        mEtPassword.getText().toString().trim(),
                        mEtPasswordConfirm.getText().toString().trim(),
                        mEtIdentifyCode.getText().toString().trim(),
                        mEtEmail.getText().toString().trim(),
                        mEtPhone.getText().toString().trim());
                break;
            case R.id.iv_back:
                //返回
                this.finish();
                break;
        }
    }

    @Override
    public void getVerificationCodeBegin() {
        mTimeCount.start();

    }

    /**
     * 获取验证码60秒计时器
     */
    private class TimeCount extends CountDownTimer {
        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            if (mBtnIdentifyCodeTimer != null) {
                mBtnIdentifyCodeTimer.setText(R.string.register_get_verification_code);
                mBtnIdentifyCodeTimer.setClickable(true);
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            if (mBtnIdentifyCodeTimer != null){
                mBtnIdentifyCodeTimer.setClickable(false);//防止重复点击
                mBtnIdentifyCodeTimer.setText(millisUntilFinished / 1000 + getString(R.string.second));
            }
        }
    }
}
